#!/usr/bin/env ruby
  require 'optparse'
  require 'webrick'
  
  class NonCachingFileHandler < WEBrick::HTTPServlet::FileHandler
    def prevent_caching(res)
      res['ETag']          = nil
      res['Last-Modified'] = Time.now + 100**4
      res['Cache-Control']  = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0'
      res['Pragma']            = 'no-cache'
      res['Expires']            = Time.now - 100**4
    end

    def do_GET(req, res)
      super
      prevent_caching(res)
    end

    def do_POST(req, res)
       res.status = 200 # Success
          File.open(req.path.to_s.sub(/\//, ''),'r') do |f|
                  @template = f.read #ERB.new(f.read)
          end
          res.body = @template
    end
    
  end

  options = {:port => 8080, :directory => Dir::pwd}
  
  OptionParser.new do |opts|
    opts.banner = "Usage: webrick [-p PORT] [-d DIRECTORY]"
    opts.on("-p PORT", Integer, "Default port #{options[:port]}.") do |p|
      options[:port] = p
    end
    
    opts.on("-d DIRECTORY", String, "Default is the current working directory.") do |d|
      options[:directory] = d
    end    
  end.parse!
  puts "Starting WEBrick...\n\n"
  puts "http://localhost:#{options[:port]} -> #{options[:directory]}\n\n"
  puts 'Press ctrl-c to shutdown WEBrick.'
  server = WEBrick::HTTPServer.new :Port => options[:port]
  server.mount "/", NonCachingFileHandler , options[:directory]
  trap('INT') do 
    print "\e[0m\r\e[0K"
    $stdout.flush
    server.stop 
  end
  server.start